import {
  View,
  Text,
  Image,
  StyleSheet,
  TouchableOpacity,
  Dimensions,
} from 'react-native';
import React, { useEffect } from 'react';
import MyTabs from '../../components/MyTabs';
import EvaluateSreen from './EvaluateSreen/EvaluateSreen';
import DirectorySreen from './DirectorySreen/DirectorySreen';
import OverviewSreen from './OverviewSreen/OverviewSreen';
import { useCourseStore } from '../../store/CourseStore';
import { useCartStore } from '../../store/CartStore';
import { cartItem } from '../../models/CartModel';
import { useToast } from '../../components/MyToast/useToasr';
import { useVideoPlayerStore } from '../../store/VideoPlayer';

const DetailseSreen = ({ route }: { route: any }) => {
  const { setVideoPlayerIndex } = useVideoPlayerStore();
  // 调用仓库获取添加购物车
  const { cartAdd } = useCartStore();
  // 获取屏幕的宽度
  const { width } = Dimensions.get('window');

  const { getCourseInfoByIdApi, courseInfo, getChapterVideoByIdApi } =
    useCourseStore();

  const { id } = route.params;
  // console.log(id);

  useEffect(() => {
    setVideoPlayerIndex(-1);
    getCourseInfoByIdApi(id);
    getChapterVideoByIdApi(id);
  }, []);

  const [activeTab, setActiveTab] = React.useState(0);
  const activeTabHandler = (index: number) => {
    setActiveTab(index);
  };

  const toast = useToast();
  // 加入购物车
  const DetailsAddCart = () => {
    // console.log('添加');
    const newObj = {
      ...courseInfo.courseInfoVo,
      num: 1,
      status: false,
    };
    cartAdd(newObj as cartItem);
    toast.success('添加成功');
  };

  const tabs = ['简介', '目录', '评价'];

  return (
    <View style={styles.container}>
      {/*  头部 */}
      <View style={styles.header}>
        <Image
          style={styles.header_img}
          source={{
            uri: courseInfo.courseInfoVo?.cover,
          }}
        />
        <View style={styles.header_con}>
          {/* 标题 */}
          <Text style={styles.header_con_title}>
            {courseInfo.courseInfoVo?.title}
          </Text>
          {/* 分类 */}
          <Text style={styles.header_con_type}>Brian | 全栈工程师,创业者</Text>
          {/* 价格 */}
          <Text style={styles.header_con_price}>
            ￥{courseInfo.courseInfoVo?.price}
          </Text>
        </View>
      </View>
      {/* 内容部分 */}
      <View style={styles.contect}>
        <View>
          <Text>内容</Text>
        </View>
        <View>
          <Text>课程难度</Text>
          <Text>进阶</Text>
        </View>
        <View>
          <Text>视频时长</Text>
          <Text>50小时</Text>
        </View>
        <View>
          <Text>学习人数</Text>
          <Text>{courseInfo.courseInfoVo?.lessonNum}人</Text>
        </View>
        <View>
          <Text>课程评分</Text>
          <Text>10.00分</Text>
        </View>
      </View>
      <View style={{ height: 10, backgroundColor: '#eee' }}></View>
      {/* tabs切换 */}
      <MyTabs tabs={tabs} activeTab={activeTab} onPress={activeTabHandler} />
      {/* 渲染的内容 */}
      <View style={{ padding: 10 }}>
        {activeTab === 0 && <OverviewSreen />}
        {activeTab === 1 && <DirectorySreen />}
        {activeTab === 2 && <EvaluateSreen />}
      </View>
      {/* 底部 */}
      <View style={[styles.bottomActions, { width: width }]}>
        <View style={styles.leftActions}>
          <TouchableOpacity style={styles.actionButton}>
            <Text style={styles.actionButtonText}>咨询</Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.actionButton}>
            <Text style={styles.actionButtonText}>试看</Text>
          </TouchableOpacity>
        </View>
        <View style={styles.rightActions}>
          <TouchableOpacity style={styles.addToCartButton}>
            <Text
              style={styles.addToCartButtonText}
              onPress={() => DetailsAddCart()}
            >
              加入购物车
            </Text>
          </TouchableOpacity>
          <TouchableOpacity style={styles.buyNowButton}>
            <Text style={styles.buyNowButtonText}>立即购买</Text>
          </TouchableOpacity>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    position: 'relative',
    flex: 1,
    backgroundColor: '#fff',
  },
  header: {
    padding: 10,
    backgroundColor: '#eee',
    paddingTop: 20,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-around',
    width: '100%',
  },
  header_img: {
    width: 80,
    height: 100,
    objectFit: 'cover',
    borderRadius: 10,
  },
  header_con: {
    width: '70%',
    marginLeft: 10,
    height: 100,
    justifyContent: 'space-around',
  },
  header_con_title: {
    fontSize: 16,
    fontWeight: 'bold',
  },
  header_con_type: {
    color: '#999',
  },
  header_con_price: {
    color: '#f00',
    fontSize: 16,
  },
  contect: {
    padding: 10,
    flexDirection: 'row',
    gap: 10,
    justifyContent: 'space-around',
  },
  // 底部
  bottomActions: {
    bottom: 0,
    left: 0,
    position: 'absolute',
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    padding: 12,
    backgroundColor: '#FFFFFF',
    borderTopWidth: 1,
    borderTopColor: '#EEEEEE',
  },
  leftActions: {
    flexDirection: 'row',
  },
  rightActions: {
    flexDirection: 'row',
  },
  actionButton: {
    paddingHorizontal: 12,
    paddingVertical: 8,
    borderWidth: 1,
    borderColor: '#CCCCCC',
    borderRadius: 4,
    marginRight: 12,
  },
  actionButtonText: {
    fontSize: 14,
    color: '#666666',
  },
  addToCartButton: {
    backgroundColor: '#FFB74D',
    paddingHorizontal: 16,
    paddingVertical: 10,
    borderRadius: 4,
    marginRight: 12,
  },
  addToCartButtonText: {
    fontSize: 14,
    color: '#FFFFFF',
    fontWeight: 'bold',
  },
  buyNowButton: {
    backgroundColor: '#FF5722',
    paddingHorizontal: 16,
    paddingVertical: 10,
    borderRadius: 4,
  },
  buyNowButtonText: {
    fontSize: 14,
    color: '#FFFFFF',
    fontWeight: 'bold',
  },
});

export default DetailseSreen;
